package com.apes.hr.base.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.jpa.generator.service.SequenceService;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.hr.base.model.PersonGroup;
import com.apes.hr.base.repository.PersonGroupRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 功能：分组
 *
 * @author Fengjiajing
 * @create 2018-08-25 14:35
 */
@Service("hrPersonGroupService")
public class PersonGroupService extends DomainService {
    @Autowired
    public PersonGroupRepository personGroupRepository;
    @Autowired
    public SequenceService sequenceService;

    public PersonGroup save(SimpleRequest request) {
        PersonGroup personGroup = request.getO(PersonGroup.class);
        return personGroupRepository.saveAndFlush(personGroup);
    }


    /**
     * 功能：下拉模糊查询使用
     * @param request
     * @return
     */
    public Page<PersonGroup> find(SimpleRequest request) {
        return personGroupRepository.find( request.get("key"), PageRequest.of(request.get("page"), request.get("size")) );
    }

    public Page<PersonGroup> findAll(SimpleRequest request) {
        List<Map> filters = request.get("condition");
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        return personGroupRepository.findAll(JpaDsl.toCriteria(filters), PageRequest.of(request.get("page"), request.get("size"), sort));
    }

    public PersonGroup findOne(String id) {
        return personGroupRepository.findOne(id);
    }
}
